
package com.fx.apartment.modules.file.controller;



import com.fx.apartment.common.tool.api.R;

import com.fx.apartment.modules.file.entity.QinFile;
import com.fx.apartment.modules.file.service.IFileService;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;

import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * 文件上传 控制器
 *
 * @author QinXueFeng
 * @since 2021-04-19
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fx-file")
@Api(value = "文件上传", tags = "文件上传接口")
@Slf4j
public class FileController {

	private IFileService fileService;

	/**
	 * 上传文件
	 *
	 * @param file 文件
	 * @return ObjectStat
	 */
	@SneakyThrows
	@PostMapping("/put-file")
	public R<QinFile> putFile(@RequestParam MultipartFile file) {
		return R.data(fileService.putFile(file));
	}



	/**
	 * 上传文件
	 *
	 * @param files 文件集合
	 * @return ObjectStat
	 */
	@SneakyThrows
	@PostMapping("/put-files")
	public R<List<QinFile>> putFile(@RequestBody List<MultipartFile> files) {
		List<QinFile> list = Lists.newArrayListWithCapacity(files.size());
		files.forEach(l->{
			list.add(fileService.putFile(l));
		});
		return R.data(list);
	}

	/**
	 * 上传文件
	 *
	 * @param dir 路径
	 * @param file     文件
	 * @return ObjectStat
	 */
	@SneakyThrows
	@PostMapping("/put-file-by-dir")
	public R<QinFile> putFile(@RequestParam String dir, @RequestParam MultipartFile file) {
		fileService.putFile(file,dir);
		return R.data(null);
	}


}
